package cn.com.tianpeidong.restaurantadmin.common.aop;

import com.google.common.base.Stopwatch;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;

import java.util.concurrent.TimeUnit;

/**
 * @author tianpeidong
 * @version 2019.01.13
 */
@Aspect
@Component
public class ApiActAop {
    private static final Logger LOGGER= LogManager.getLogger();


    @Around("execution(* cn.com.tianpeidong.restaurantadmin.project.*.rest.*Controller.*(..)) ")
    public Object doBasicProfiling(ProceedingJoinPoint pjp) throws Throwable {
        StringBuilder name = null;
        Stopwatch stopwatch = Stopwatch.createStarted();
        try {
            name = new StringBuilder(pjp.getTarget().getClass().getSimpleName()).append(":").append(pjp.getSignature().getName());
            Object o = pjp.proceed();
            return o;
        } catch (Throwable throwable) {
            LOGGER.error("接口：" + name + "抛异常");
            throw throwable;
        } finally {
            LOGGER.info("接口:{}，耗时:{}ms",
                    name,
                    stopwatch.elapsed(TimeUnit.MILLISECONDS));
        }
    }
}